package com.java.manage.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.java.manage.mapper.TbContentCategoryMapper;
import com.java.manage.pojo.TbContentCategory;
import com.java.manage.service.TbContentCategoryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName TbContentCategoryServiceImpl
 * @Description 前台广告服务
 * @Author Trigger Wu
 * @Date 2018/9/1116:33
 * @Version 1.0
 **/
@Service
public class TbContentCategoryServiceImpl extends ServiceImpl<TbContentCategoryMapper,TbContentCategory> implements TbContentCategoryService {

    /*
     *  删除节点，如果是主节点则删除全部子节点
     *  程序入口
     *
     * @author Tigger Wu
     * @date 2018/9/11 17:48
     * @param [category]
     * @return void
     */
    public void deleteAllNode(TbContentCategory category) {
        //删除此节点（修改状态）
        this.queryAllNode(category.getId());
        //判断是否更新父节点
        this.updateParentNode(category.getParentId());
    }
    /*
    *
     *  查询全部节点并删除
     *
     * @author Tigger Wu
     * @date 2018/9/11 17:47
     * @param [ids, pid]
     * @return void
     */
    public void queryAllNode(Long pid) {
        List<Object> ids = new ArrayList<Object>();
        EntityWrapper<TbContentCategory>wrapper = new EntityWrapper<TbContentCategory>();
        wrapper.where("parent_id={0}",pid);
        List<TbContentCategory> selectList = super.selectList(wrapper);
        EntityWrapper<TbContentCategory>wrapper1 = new EntityWrapper<TbContentCategory>();
        wrapper1.where("id={0}",pid);
        //得到父节点
        TbContentCategory deleteNode = super.selectOne(wrapper1);
        //获取父节点id
        Long parentId = deleteNode.getParentId();
        //删除父节点(修改状态)
        deleteNode.setStatus(2);
        super.updateById(deleteNode);
        //如果此节点下面没有子节点
        if(0==selectList.size()||selectList.isEmpty()){
            //删除父节点后，判断此节点上的父节点是否为子节点，是，更新父节点为子节点(修改状态)
            this.updateParentNode(parentId);
        }else{
            for(TbContentCategory category:selectList){
                if(category.getIsParent()==1){
                    //递归删除节点(修改状态)
                    this.queryAllNode(category.getId());
                }else {
                    category.setStatus(2);
                    super.updateById(category);
                }
            }
        }
    }
    /*
     *  判断此节点是否存在兄弟节点，若不存在，则将其父节点更新成子节点
     * 
     * @author Tigger Wu
     * @date 2018/9/12 18:36
     * @param [parentId]
     * @return void
     */
    private void updateParentNode(Long parentId) {
        //查询此节点的所有的兄弟节点
        EntityWrapper<TbContentCategory>wrapper1 = new EntityWrapper<TbContentCategory>();
        wrapper1.where("parent_id={0}",parentId);
        List<TbContentCategory> categories = super.selectList(wrapper1);
        //若无兄弟节点
        if (categories.size() == 0) {
            //更新此节点的父节点为子节点
            EntityWrapper<TbContentCategory>wrapper2 = new EntityWrapper<TbContentCategory>();
            wrapper1.where("id={0}",parentId);
            TbContentCategory node2 = super.selectOne(wrapper2);
            node2.setIsParent(0);
            EntityWrapper<TbContentCategory>wrapper3 = new EntityWrapper<TbContentCategory>();
            wrapper1.where("id={0}",node2.getId());
            super.update(node2,wrapper3);
        }
    }
    /*
    *
     *  增加节点
     *
     * @author Tigger Wu
     * @date 2018/9/11 17:47
     * @param [category]
     * @return void
     */
    public void saveNode(TbContentCategory category) {
        //禁止输入值
        category.setId(null);
        category.setIsParent(0);
        category.setSortOrder(1);
        category.setStatus(1);
        Date d  = new Date();
        category.setCreated(d);
        category.setUpdated(d);
        super.insert(category);
        //判断该节点父节点字段Isparent是否为true,不是就修改为true
        TbContentCategory parent =  super.selectById(category.getParentId());
        if(parent.getIsParent()!=1){
            parent.setIsParent(1);
            super.updateById(parent);
        }
    }

}
